package site.wanjiahao.handler.client;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import site.wanjiahao.message.request.PingMessage;

public class MyChannelDuplexHandler extends ChannelDuplexHandler {

    // 触发自定义事件
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        IdleStateEvent event = (IdleStateEvent) evt;
        if (event.state() == IdleState.WRITER_IDLE) {
            // 触发读空闲事件，向服务器发送一个心跳包层序, 证明此TCP连接正常
            PingMessage pingMessage = new PingMessage();
            ctx.writeAndFlush(pingMessage);
        }
    }
}
